const app = getApp();
var WxParse = require('../../../utils/wxParse/wxParse.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    taskDetail: null,
  },
  showModal: function () {
    this.setData({
      showModal: true
    })
  },
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
  showTasks: function () {
    wx.switchTab({
      url: '../task_list/task_list',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //options.id 为task的id， 需要网络请求任务详情 （带token）
    var _this = this;
    wx.request({
      url: app.globalData.apiDomain + 'crowdsource/project/getProjectDetails/' + options.id,
      header: { 'Content-Type': 'application/json', 'token': app.globalData.token },
      success: function (res) {
        if (res.data.code == '200') {
          var basicRequirements;
          var projectDescription;
          basicRequirements = res.data.object.basicRequirements ? res.data.object.basicRequirements : '--';
          projectDescription = res.data.object.projectDescription ? res.data.object.projectDescription : '--';
          WxParse.wxParse('basicRequirements', 'html', basicRequirements, _this, 15);
          WxParse.wxParse('projectDescription', 'html', projectDescription, _this, 15);
          _this.setData({
            taskDetail: res.data.object
          })
        } else {
          wx.showModal({
            title: '获取任务失败',
            showCancel: false,
            content: '失败信息:' + res.data.errorMsg + '    请重试!',
            success: function (res) {

            }
          })
        }
      },
      fail: function (e) {
        console.log('task fail');
        wx.showModal({
          title: '获取任务失败',
          showCancel: false,
          content: e,
          success: function (res) {
            if (res.confirm) {
            }
          }
        })
      }
    })
  },
  applyTask: function (e) {
    var _this = this;
    var taskId = e.currentTarget.dataset.taskId;
    if (app.globalData.token == null) {
      app.login(_this, '/pages/public_tasks/task_detail/task_detail?id=' + taskId);
    } else {
      this.applyTaskRequest(e);
    }

    app.userLoginReadyCallback = res => {
      this.applyTaskRequest(e)
    }
  },
  applyTaskRequest: function (e) {
    let _this = this;
    let projectId = e.target.dataset.taskId;

    wx.request({
      url: app.globalData.apiDomain + 'crowdsource/task/applyTask',
      header: { 'Content-Type': 'application/json', 'token': app.globalData.token },
      method: 'POST',
      data: {
        projectId: projectId,
        //acceptUserId: 2
      },
      success: function (res) {
        var data = res.data;
        let projectTypeId = _this.data.taskDetail.projectTypeId;
        let projectName = _this.data.taskDetail.projectName;

        let prescription = _this.data.taskDetail.prescription;
        let msg = '';
        if (prescription) {
          msg = '请在' + prescription + '小时之内完成任务';
        }
        if (data.status === 'ok') {
          if (msg) {
            wx.showModal({
              title: '申请成功',
              content: msg,
              showCancel: false,
              success: function (e) {
                var taskId = data.object;

                //判断项目类型
                if (projectTypeId == 2) {
                  wx.navigateTo({
                    url: '../do_task/do_task?id=' + taskId + '&projectName=' + projectName,
                  })
                } else if (projectTypeId == 3) {
                  wx.navigateTo({
                    url: '../do_picture_task/do_picture_task?id=' + taskId + '&projectName=' + projectName,
                  })
                }

              }
            })
          } else {
            wx.showToast({
              title: '申请成功',
              icon: 'success',
              duration: 3000,
              complete: function () {
                var taskId = data.object;
                setTimeout(function () {
                  //判断项目类型
                  if (projectTypeId == 2) {
                    wx.navigateTo({
                      url: '../do_task/do_task?id=' + taskId + '&projectName=' + projectName,
                    })
                  } else if (projectTypeId == 3) {
                    wx.navigateTo({
                      url: '../do_picture_task/do_picture_task?id=' + taskId + '&projectName=' + projectName,
                    })
                  }
                }, 2000)
              }
            });
          }


        }else {
          app.responseCheck(_this, data, '/pages/my_tasks/task_list/task_list')
        }
      },
      fail: function (e) {
        wx.showModal({
          content: '不好，挂了！管理员哥哥正在赶来~',
          showCancel: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function (res) {
  //   var task = this.data.taskDetail;
  //   console.log(res);
  //   return {
  //     title: task.title,
  //     path: '/pages/public_tasks/task_detail/task_detail?id=' + task.id,
  //     success: function (res) {
  //       // 转发成功
  //     },
  //     fail: function (res) {
  //       // 转发失败
  //     }
  //   }
  // }
})

